#include"SchoolBus.h"
//校车导航功能
//分三个部分：1.从当前地点到公交车站  2.坐车过程   3.下车后到目的地的过程


bool BusNavigation(Dot* source, Dot* target,double &walkinglength1, double& Buslength, double& walkinglength2,vector<Dot*>&path1, vector<Dot*>& path2, vector<Dot*>& path3) {   //校车导航，获取路径以及总距离(均分为三段：上车前，乘车，下车后）
	//Dot* GetNearestStation(Dot * source, vector<Dot*>&path, double& length)
	Dot* GettingonStation = GetNearestStation(source, path1, walkinglength1);   //上车站点
	Dot* GettingdownStation = GetNearestStation(target, path3, walkinglength2); ;    //下车站点
	reverse(path3.begin(), path3.end());
	int i = BusKu[0].GetStationIndex(GettingonStation);
	int j= BusKu[0].GetStationIndex(GettingdownStation);   //函数返回1表示正向，返回2表示反向
	walkinglength2 = 0;
	if (i < j) {
		for (int r = i; r <= j; r++) {
			path2.push_back(BusKu[0].Station[r]);
			if (r < j) {
				double minDistance=0;
				Floyd(BusKu[0].Station[r], BusKu[0].Station[r + 1], minDistance);
				walkinglength2 += minDistance;
			}
		}
		return 1;
	}
	else {
		for (int r = j; r <= i; r++) {
			path2.push_back(BusKu[0].Station[r]);
			if (r < i) {
				double minDistance = 0;
				Floyd(BusKu[0].Station[r], BusKu[0].Station[r + 1], minDistance);
				walkinglength2 += minDistance;
			}
		}
		return 0;
	}
}

Time minn(Time a, Time b) {
	return a < b ? a : b;
}

void CaltulateBusRootTime(Dot* source, Dot* target, Time& ArriveTime, Time& ProcessTime, Time& WaitingTime, double speed) {   //传参说明：前两个参数表示起点和目的地，speed传校车的速度常量,其他变量为引用传递,分别表示到达时间，总共花费时间和等车时间
	double walkinglength1; double Buslength; double walkinglength2; vector<Dot*> path1; vector<Dot*> path2; vector<Dot*> path3;
	vector<Dot*>path; double length;
	Dot* GettingonStation = GetNearestStation(source, path, length);
	bool Direction=BusNavigation(source, target, walkinglength1, Buslength, walkinglength2, path1, path2, path3);
	Time ProcessTime1;
	Time ProcessTime2;
	Time ProcessTime3;
	int CrossroadSum1;
	int CrossroadSum2;
	int CrossroadSum3;
	CalculateRootTime(path1, ArriveTime, ProcessTime1, WalkingVelocity, CrossroadSum1);   //上车前步行
	if (Direction == 1) {
		WaitingTime =(ArriveTime - (BusKu[0].StationTime[GettingonStation])[0]) % BusKu[0].BusDuration;
		for (int i = 2;i< (BusKu[0].StationTime[GettingonStation]).size(); i += 2) {
			WaitingTime = minn(ArriveTime,(ArriveTime - (BusKu[0].StationTime[GettingonStation])[i]) % BusKu[0].BusDuration);
		}
	}
	else {
		WaitingTime = (ArriveTime - (BusKu[0].StationTime[GettingonStation])[1]) % BusKu[0].BusDuration;
		for (int i = 1; i < (BusKu[0].StationTime[GettingonStation]).size(); i += 2) {
			WaitingTime = minn(ArriveTime, (ArriveTime - (BusKu[0].StationTime[GettingonStation])[i]) % BusKu[0].BusDuration);
		}
	}       //计算等车的时间
	CalculateRootTime(path2, ArriveTime, ProcessTime2, BusVelocity, CrossroadSum2);    //乘公交
	CalculateRootTime(path3, ArriveTime, ProcessTime3, WalkingVelocity, CrossroadSum3);   //下车后步行
	ProcessTime = ProcessTime1 + ProcessTime2 + ProcessTime3+ WaitingTime;
	ArriveTime = GetSystemTime() + ProcessTime;
	return;
}